package com.example.chaindemo.web.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;
import java.util.concurrent.CopyOnWriteArraySet;

@Slf4j
@Component
@EnableScheduling
public class TimeTask {

    @Scheduled(cron = "0/1 * * * * ?")
    public void test() {

        System.err.println("*********   定时任务执行   **************");

        CopyOnWriteArraySet<MyWebSocket> webSocketSet = MyWebSocket.getWebSocketSet();

        int i = 0;
        webSocketSet.forEach(
                c -> {
                    try {
                        c.sendMessage("  定时发送  " + new Date().toLocaleString());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                });

        System.err.println("/n 定时任务完成.......");
    }
}